#include<bits/stdc++.h>
using namespace std;
char a[55][55];
void solve(){
    int n,m,r,c,pd=0;
    cin>>n>>m>>r>>c;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            {cin>>a[i][j];if(a[i][j]=='B')pd=1;}
    if(a[r][c]=='B'){cout<<0<<"\n";return;}
    for(int j=1;j<=m;j++)
        if(a[r][j]=='B'){cout<<1<<"\n";return;}
    for(int i=1;i<=n;i++)
        if(a[i][c]=='B'){cout<<1<<"\n";return;}
    if(pd)cout<<2<<"\n";
    else cout<<-1<<"\n";
}
int main () {
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)solve();
}